<?php
namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\Db;
use think\Session;

class CommonController extends Controller
{
    //初始化操作
    protected function _initialize()
    {
        parent::_initialize();
        $user = Session::get('user_id');
        if (empty($user)) $user = Cookie::get('user_id');
        if( empty($user) ){
            $this->redirect('login/index');
        }

        //1.取出轮播图
        $lunbo = Db::name('lunbo')->order('sort_order desc')->select();
        $this->assign('lunbo', $lunbo);
    }

    //搜索功能实现
    public function search()
    {
        $keywords = input('get.keyword');
        $like = '%'.$keywords.'%';
        $list = Db::name('book')->where('title|author|description', 'like', $like)->limit(10)->select();
        $this->assign('list', $list);
        //dump($keywords);
        return $this->fetch();
    }
}
